/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.rmatos.programming.blog.presentation;

import com.rmatos.programming.blog.entities.Tags;
import com.rmatos.programming.blog.facade.CommentSessionBeanLocal;
import java.util.ArrayList;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;

/**
 *
 * @author User
 */
@ManagedBean(name = "CommentManagedBean")
@SessionScoped
public class CommentManagedBean {

    @EJB
    private CommentSessionBeanLocal commentSessionBean;
    private String title;
    private String owner;
    private String category;
    private String body;
    private String tags;

    public String getBody() {
        return body;
    }

    public void setBody(String body) {
        this.body = body;
    }

    public String getCategory() {
        return category;
    }

    public void setCategory(String category) {
        this.category = category;
    }

    public String getOwner() {
        return owner;
    }

    public void setOwner(String owner) {
        this.owner = owner;
    }

    public String getTags() {
        return tags;
    }

    public void setTags(String tags) {
        this.tags = tags;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    /** Creates a new instance of CommentManagedBean */
    public CommentManagedBean() {
    }

    public String commitComment() {

        String _returnPage = "";

        HttpSession session = ((HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false));

        if (session != null) {

            String _tempUsername = session.getAttribute("username").toString();
            System.out.println("USERNAME:"+_tempUsername);
            commentSessionBean.createNewComment(_tempUsername,category,title, body, splitTags());
            _returnPage = "index";

        } else {

            _returnPage = "error";
        }


        return _returnPage;
    }

    private ArrayList<Tags> splitTags() {

        ArrayList<Tags> _tempTags = new ArrayList<Tags>();
        
        if (tags.length() > 0) {


            String[] _tagsNames = tags.split(",");
            int _numberOfTags = _tagsNames.length;

            for (int i = 0; i < _numberOfTags; i++) {

                _tempTags.add(new Tags(_tagsNames[i].trim()));


            }

        }

        return _tempTags;


    }

    public ArrayList<String> getAddCommentsCategories() {

        ArrayList<String> _tempCategories = new ArrayList<String>();

        //TODO: Change this
        _tempCategories.add("Java World");
        _tempCategories.add("Iphone Development");
        _tempCategories.add("Other Projects");
        _tempCategories.add("Music");

        return _tempCategories;


    }
}
